﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;
using System.Data;

namespace MusaDor.Publisher
{
    public partial class AddSong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public int returnValue { get; set; }

        protected void Send_Click(object sender, EventArgs e)
        {
            Boolean Approved = false;
            String AddedBy = User.Identity.Name;
            String connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            String FullPath = Server.MapPath("~/Music/");
            Boolean FullFileOK = false;
            Boolean FullSongOk = false;
            int CategoryID = 13;

            if (IsPostBack)
            {
                if (SongUpload.HasFile)
                {
                    String fileExtension =
                        System.IO.Path.GetExtension(SongUpload.FileName).ToLower();
                    String[] allowedExtensions = { ".mp3" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            FullFileOK = true;
                        }
                    }
                }

                if (FullFileOK)
                {
                    try
                    {
                        FullSongOk = true;
                    }
                    catch (Exception exf)
                    {
                        SongUploadStatus.Text = "Error";
                    }
                }
                else
                {
                    SongUploadStatus.Text = "Error";
                }
            }

            if (FullSongOk == true)
            {
                Guid NewSongFileName = Guid.NewGuid();
                String SongfileExtension = Path.GetExtension(SongUpload.FileName);
                String FullSongFileName = FullPath + NewSongFileName + SongfileExtension;
                String ShortSongFileName = NewSongFileName + SongfileExtension;
                SongUpload.PostedFile.SaveAs(FullSongFileName);
                Send.PostBackUrl = "#Successfully";

                using (SqlConnection conn = new SqlConnection(connStr))
                {

                    string sql = "INSERT INTO md_Articles ([Title],[CategoryID],[FileName],[Approved],[AddedBy]) VALUES " + "(@Title,@CategoryID,@FileName,@Approved,@AddedBy)";

                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {

                        SqlParameter[] prms = new SqlParameter[5];
                        prms[0] = new SqlParameter("@Title", SqlDbType.NVarChar, 256);
                        prms[0].Value = TextBoxTitle.Text.Trim();
                        prms[1] = new SqlParameter("@CategoryID", SqlDbType.Int, 4);
                        prms[1].Value = CategoryID;
                        prms[2] = new SqlParameter("@Approved", SqlDbType.Bit);
                        prms[2].Value = Approved;
                        prms[3] = new SqlParameter("@FileName", SqlDbType.NVarChar, 256);
                        prms[3].Value = ShortSongFileName;
                        prms[4] = new SqlParameter("@AddedBy", SqlDbType.NVarChar, 256);
                        prms[4].Value = AddedBy;
                        cmd.Parameters.AddRange(prms);
                        conn.Open();
                        returnValue = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
            }
        }
    }
}